while True:
    v = int(input())
    if len(str(v)) < 4:
        print("0")

    else:
        break
m1 = v
m2 = v // 1000
m3 = v // 100
m4 = v // 10
m5 = v % 1000
m6 = v % 10
m7 = (v // 10) % 10
m8 = (v // 100) % 10
sp = [m1, m2, m3, m4, m5, m6, m7, m8]
spres = []
fl = 0
for i in range(0, 8):
    n = sp[i]
    d = 0
    for ch in range(2,n // 2 + 1):
        if n % ch == 0:
            d = 1
            break
    if d == 0 and n != 1 and n != 0:
        spres.append(n)
        fl = 1
if fl == 1:
    print(max(spres))
else:
    print(0)
